#!/bin/sh

. libtest.sh
. libgit.sh

executable exec-env <<EOF
#!/bin/sh

{
	echo "\$TEST_CASE"
	printf ' - pwd='; pwd
	for arg in git-dir show-cdup is-inside-work-tree show-superproject-working-tree; do
		echo " - \$arg=\$(git rev-parse --\$arg)"
	done
} | sed "s,$output_dir,ROOT," >> $output_dir/exec-env
EOF

tigrc <<EOF
set show-untracked = no
set line-graphics = ascii
set diff-view-line-number = yes
set stage-view-line-number = yes
bind diff <C-p> !sh -c 'TEST_CASE=\$TEST_CASE::exec exec-env'
EOF

script='
	:210
	:scroll-line-down
	:edit

	:224
	:exec !vim +%(lineno) %(file)
'

setup_submodule_project()
{
	mkdir -p "$output_dir/base"
	cd "$output_dir/base"
	create_repo_from_tgz "$base_dir/files/scala-js-benchmarks.tgz"
	cd "$output_dir"
	git_init
	cd "$output_dir/$work_dir"
	git -c protocol.file.allow=always submodule add "$output_dir/base" bench
  git_commit -m "Adding benchmark"
}

test_exec_work_dir setup_submodule_project
work_dir="$work_dir/bench"

test_case default \
 --args='show ee912870202200a0b9cf4fd86ba57243212d341e' \
 --before='exec-env' \
 --script="$script" <<EOF
   | -  )
   | -
   |    lazy val parent: Project = Project(
200|        id = "parent",
   |        base = file("."),
   | @@ -53,10 +48,10 @@ object ScalaJSBenchmarks extends Build {
   |    )
   |
205|    lazy val common = project("Common", defaultSettings)
   | -  lazy val deltablue = project("DeltaBlue", benchmarkSettings).dependsOn(c
   | -  lazy val richards = project("Richards", benchmarkSettings).dependsOn(com
   | -  lazy val sudoku = project("Sudoku", benchmarkSettings).dependsOn(common)
   | -  lazy val tracer = project("Tracer", benchmarkSettings).dependsOn(common)
210| +  lazy val deltablue = project("DeltaBlue", defaultSettings).dependsOn(com
   | +  lazy val richards = project("Richards", defaultSettings).dependsOn(commo
   | +  lazy val sudoku = project("Sudoku", defaultSettings).dependsOn(common)
   | +  lazy val tracer = project("Tracer", defaultSettings).dependsOn(common)
   |
215|    def project(id: String, settings: Seq[sbt.Def.Setting[_]]) = Project(
   |        id = id.toLowerCase,
   | diff --git a/project/build.sbt b/project/build.sbt
   | index 87c2e66..122abd0 100644
   | --- a/project/build.sbt
220| +++ b/project/build.sbt
   | @@ -1 +1,4 @@
   | -addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.
   | +resolvers += Resolver.url("scala-js-snapshots",
   | +    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatt
[diff] Changes to 'project/build.sbt' - line 224 of 367                    61%
EOF

test_case sub-directory \
 --args='show ee912870202200a0b9cf4fd86ba57243212d341e' \
 --before='mkdir -p some/subdir && cd some/subdir && exec-env' \
 --script="
    $script
    <C-p>
" <<EOF
   | -  )
   | -
   |    lazy val parent: Project = Project(
200|        id = "parent",
   |        base = file("."),
   | @@ -53,10 +48,10 @@ object ScalaJSBenchmarks extends Build {
   |    )
   |
205|    lazy val common = project("Common", defaultSettings)
   | -  lazy val deltablue = project("DeltaBlue", benchmarkSettings).dependsOn(c
   | -  lazy val richards = project("Richards", benchmarkSettings).dependsOn(com
   | -  lazy val sudoku = project("Sudoku", benchmarkSettings).dependsOn(common)
   | -  lazy val tracer = project("Tracer", benchmarkSettings).dependsOn(common)
210| +  lazy val deltablue = project("DeltaBlue", defaultSettings).dependsOn(com
   | +  lazy val richards = project("Richards", defaultSettings).dependsOn(commo
   | +  lazy val sudoku = project("Sudoku", defaultSettings).dependsOn(common)
   | +  lazy val tracer = project("Tracer", defaultSettings).dependsOn(common)
   |
215|    def project(id: String, settings: Seq[sbt.Def.Setting[_]]) = Project(
   |        id = id.toLowerCase,
   | diff --git a/project/build.sbt b/project/build.sbt
   | index 87c2e66..122abd0 100644
   | --- a/project/build.sbt
220| +++ b/project/build.sbt
   | @@ -1 +1,4 @@
   | -addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.
   | +resolvers += Resolver.url("scala-js-snapshots",
   | +    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatt
[diff] Changes to 'project/build.sbt' - line 224 of 367                    61%
EOF

test_case sub-directory-diffstat \
 --args='show ee912870202200a0b9cf4fd86ba57243212d341e' \
 --before='mkdir -p some/subdir && cd some/subdir && exec-env' \
 --script='
	:10
	:edit
	:26
	:exec !vim %(file)
	<C-p>
' <<EOF
  1| commit ee912870202200a0b9cf4fd86ba57243212d341e
   | Refs: [master], {origin/master}, {origin/HEAD}
   | Author:     Jonas Fonseca <jonas.fonseca@gmail.com>
   | AuthorDate: Sat Mar 1 17:26:01 2014 -0500
  5| Commit:     Jonas Fonseca <jonas.fonseca@gmail.com>
   | CommitDate: Sat Mar 1 17:26:01 2014 -0500
   |
   |     WIP: Upgrade to 0.4-SNAPSHOT and DCE
   | ---
 10|  common/benchmark-runner.sh                                  |  5 +++--
   |  common/src/main/scala/org/scalajs/benchmark/Benchmark.scala | 11 +++++----
   |  .../src/main/scala/org/scalajs/benchmark/BenchmarkApp.scala |  2 +-
   |  common/start-benchmark.js                                   |  9 +++++++--
   |  deltablue/exports.js                                        | 13 ---------
 15|  .../scala/org/scalajs/benchmark/deltablue/DeltaBlue.scala   |  7 +++++++
   |  project/Build.scala                                         | 13 ++++-----
   |  project/build.sbt                                           |  5 ++++-
   |  richards/exports.js                                         | 13 ---------
   |  .../scala/org/scalajs/benchmark/richards/Richards.scala     |  3 +++
 20|  run.sh                                                      |  2 +-
   |  sudoku/exports.js                                           | 13 ---------
   |  .../main/scala/org/scalajs/benchmark/sudoku/Sudoku.scala    |  2 ++
   |  tracer/exports.js                                           | 13 ---------
   |  tracer/index-dev.html                                       |  2 +-
 25|  tracer/index.html                                           |  2 +-
   |  .../main/scala/org/scalajs/benchmark/tracer/Tracer.scala    |  3 +++
   |  17 files changed, 42 insertions(+), 76 deletions(-)
   |
[diff] Press '<Enter>' to jump to file diff - line 26 of 367                  7%
EOF

# This settings doesn't seem to apply to `git-show`
test_case mnemonic \
 --args='show ee912870202200a0b9cf4fd86ba57243212d341e' \
 --before='git config diff.mnemonicPrefix true' \
 --after='git config diff.mnemonicPrefix false' \
 --script="$script" << EOF
   | -  )
   | -
   |    lazy val parent: Project = Project(
200|        id = "parent",
   |        base = file("."),
   | @@ -53,10 +48,10 @@ object ScalaJSBenchmarks extends Build {
   |    )
   |
205|    lazy val common = project("Common", defaultSettings)
   | -  lazy val deltablue = project("DeltaBlue", benchmarkSettings).dependsOn(c
   | -  lazy val richards = project("Richards", benchmarkSettings).dependsOn(com
   | -  lazy val sudoku = project("Sudoku", benchmarkSettings).dependsOn(common)
   | -  lazy val tracer = project("Tracer", benchmarkSettings).dependsOn(common)
210| +  lazy val deltablue = project("DeltaBlue", defaultSettings).dependsOn(com
   | +  lazy val richards = project("Richards", defaultSettings).dependsOn(commo
   | +  lazy val sudoku = project("Sudoku", defaultSettings).dependsOn(common)
   | +  lazy val tracer = project("Tracer", defaultSettings).dependsOn(common)
   |
215|    def project(id: String, settings: Seq[sbt.Def.Setting[_]]) = Project(
   |        id = id.toLowerCase,
   | diff --git a/project/build.sbt b/project/build.sbt
   | index 87c2e66..122abd0 100644
   | --- a/project/build.sbt
220| +++ b/project/build.sbt
   | @@ -1 +1,4 @@
   | -addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.
   | +resolvers += Resolver.url("scala-js-snapshots",
   | +    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatt
[diff] Changes to 'project/build.sbt' - line 224 of 367                    61%
EOF

test_case noprefix \
 --args='show ee912870202200a0b9cf4fd86ba57243212d341e' \
 --before='git config diff.noprefix true' \
 --after='git config diff.noprefix false' \
 --script="$script" << EOF
   | -  )
   | -
   |    lazy val parent: Project = Project(
200|        id = "parent",
   |        base = file("."),
   | @@ -53,10 +48,10 @@ object ScalaJSBenchmarks extends Build {
   |    )
   |
205|    lazy val common = project("Common", defaultSettings)
   | -  lazy val deltablue = project("DeltaBlue", benchmarkSettings).dependsOn(c
   | -  lazy val richards = project("Richards", benchmarkSettings).dependsOn(com
   | -  lazy val sudoku = project("Sudoku", benchmarkSettings).dependsOn(common)
   | -  lazy val tracer = project("Tracer", benchmarkSettings).dependsOn(common)
210| +  lazy val deltablue = project("DeltaBlue", defaultSettings).dependsOn(com
   | +  lazy val richards = project("Richards", defaultSettings).dependsOn(commo
   | +  lazy val sudoku = project("Sudoku", defaultSettings).dependsOn(common)
   | +  lazy val tracer = project("Tracer", defaultSettings).dependsOn(common)
   |
215|    def project(id: String, settings: Seq[sbt.Def.Setting[_]]) = Project(
   |        id = id.toLowerCase,
   | diff --git project/build.sbt project/build.sbt
   | index 87c2e66..122abd0 100644
   | --- project/build.sbt
220| +++ project/build.sbt
   | @@ -1 +1,4 @@
   | -addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.
   | +resolvers += Resolver.url("scala-js-snapshots",
   | +    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatt
[diff] Changes to 'project/build.sbt' - line 224 of 367                    61%
EOF

test_case default-conflict \
 --before='setup-conflict.sh > config.setup' \
 --script='
 	:view-diff
	:7
	:edit

	:10
	:exec !vim +%(lineno) %(file)
' << EOF
  1| diff --cc conflict-file
   | index 86c5a05,b4c3de6..0000000
   | --- a/conflict-file
   | +++ b/conflict-file
  5| @@@ -1,1 -1,1 +1,5 @@@
   | ++<<<<<<< HEAD
   |  +c'
   | ++=======
   | + d'
 10| ++>>>>>>> conflict-branch


















[stage] Unstaged changes to 'conflict-file' - line 10 of 10                 100%
EOF

test_case noprefix-conflict \
 --before='git config diff.noprefix true' \
 --after='git config diff.noprefix false' \
 --script='
 	:view-diff
	:7
	:edit

	:10
	:exec !vim +%(lineno) %(file)
' << EOF
  1| diff --cc conflict-file
   | index 86c5a05,b4c3de6..0000000
   | --- conflict-file
   | +++ conflict-file
  5| @@@ -1,1 -1,1 +1,5 @@@
   | ++<<<<<<< HEAD
   |  +c'
   | ++=======
   | + d'
 10| ++>>>>>>> conflict-branch


















[stage] Unstaged changes to 'conflict-file' - line 10 of 10                 100%
EOF

run_test_cases

assert_equals 'editor.log' <<EOF
+52 project/Build.scala
  lazy val richards = project("Richards", defaultSettings).dependsOn(common)
+2 project/build.sbt
    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatterns)
+52 project/Build.scala
  lazy val richards = project("Richards", defaultSettings).dependsOn(common)
+2 project/build.sbt
    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatterns)
common/benchmark-runner.sh
#!/bin/sh
#                     __
#     ________ ___   / /  ___      __ ____  Scala.js Benchmarks
#    / __/ __// _ | / /  / _ | __ / // __/  (c) 2013, Jonas Fonseca
#  __\\ \\/ /__/ __ |/ /__/ __ |/_// /_\\ \\
tracer/src/main/scala/org/scalajs/benchmark/tracer/Tracer.scala
/*                     __                                               *\\
**     ________ ___   / /  ___      __ ____  Scala.js Benchmarks        **
**    / __/ __// _ | / /  / _ | __ / // __/  Adam Burmister             **
**  __\\ \\/ /__/ __ |/ /__/ __ |/_// /_\\ \\    2012, Google, Inc          **
** /____/\\___/_/ |_/____/_/ | |__/ /____/    2013, Jonas Fonseca        **
+52 project/Build.scala
  lazy val richards = project("Richards", defaultSettings).dependsOn(common)
+2 project/build.sbt
    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatterns)
+52 project/Build.scala
  lazy val richards = project("Richards", defaultSettings).dependsOn(common)
+2 project/build.sbt
    url("http://repo.scala-js.org/repo/snapshots/"))(Resolver.ivyStylePatterns)
+2 conflict-file
c'
+5 conflict-file
>>>>>>> conflict-branch
+2 conflict-file
c'
+5 conflict-file
>>>>>>> conflict-branch
EOF

assert_equals 'exec-env' <<EOF
default
 - pwd=ROOT/work-dir/bench
 - git-dir=ROOT/work-dir/.git/modules/bench
 - show-cdup=
 - is-inside-work-tree=true
 - show-superproject-working-tree=ROOT/work-dir
sub-directory
 - pwd=ROOT/work-dir/bench/some/subdir
 - git-dir=ROOT/work-dir/.git/modules/bench
 - show-cdup=../../
 - is-inside-work-tree=true
 - show-superproject-working-tree=ROOT/work-dir
sub-directory::exec
 - pwd=ROOT/work-dir/bench
 - git-dir=ROOT/work-dir/.git/modules/bench
 - show-cdup=
 - is-inside-work-tree=true
 - show-superproject-working-tree=ROOT/work-dir
sub-directory-diffstat
 - pwd=ROOT/work-dir/bench/some/subdir
 - git-dir=ROOT/work-dir/.git/modules/bench
 - show-cdup=../../
 - is-inside-work-tree=true
 - show-superproject-working-tree=ROOT/work-dir
sub-directory-diffstat::exec
 - pwd=ROOT/work-dir/bench
 - git-dir=ROOT/work-dir/.git/modules/bench
 - show-cdup=
 - is-inside-work-tree=true
 - show-superproject-working-tree=ROOT/work-dir
EOF
